<?php
/**
 * Minhlog Domain_Model 友情链接类
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 *
 * @category   Domain
 * @package    Domain_Model
 * @copyright  Copyright (c) 2009-2010 Minh.Shu@gmail.com
 * @license    New BSD License
 * @author     Minh.Shu
 * @version    $Id: Link.php 2 2010-01-17 13:49:08Z Minh.Shu $
 *
 */

class Domain_Model_Link
{
    private $_linkDataMapper;
    private $_linkTypeDataMapper;
    private $_linkEntity;
    private $_linkTypeEntity;

    public function  __construct()
    {
        $this->_linkDataMapper = new Domain_DataMapper_Link();
        $this->_linkEntity = new Domain_Entity_Link();
        $this->_linkDataMapper->setEntity($this->_linkEntity);
        $this->_linkTypeDataMapper = new Domain_DataMapper_Link_Type();
        $this->_linkTypeEntity = new Domain_Entity_Link_Type();
        $this->_linkTypeDataMapper->setEntity($this->_linkTypeEntity);
    }

    /**
     * 根据主键ID查询友情链接信息
     * @param integer $linkId
     * @return object|void
     */
    public function getLinkById($linkId)
    {
        $linkId = (int) $linkId;
        $resultSet = $this->_linkDataMapper->findByPrimaryId($linkId);
        return $resultSet;
    }

    /**
     * 根据主键ID查询友情链接分类信息
     * @param integer $linkTypeId
     * @return object|void
     */
    public function getLinkTypeById($linkTypeId)
    {
        $linkTypeId = (int) $linkTypeId;
        $resultSet = $this->_linkTypeDataMapper->findByPrimaryId($linkTypeId);
        return $resultSet;
    }

    /**
     * 查询全部友情链接
     * @return object|void
     */
    public function getAllLink()
    {
        return $this->_linkDataMapper->findAllLink();
    }

    /**
     * 查询全部友情链接类型
     * @return object|void
     */
    public function getAllLinkType()
    {
        $linkTypeSet = $this->_linkTypeDataMapper->findAllType(false);
        $resultSet = array();
        foreach ($linkTypeSet as $linkType) {
            $type = $this->_linkTypeDataMapper->mapper($linkType);
            $resultSet[] = $type;
        }
        return $resultSet;
    }

    /**
     * 查询友情链接树
     * @return object|void
     */
    public function getLinkTree()
    {
        $linkTypeSet = $this->_linkTypeDataMapper->findAllType();
        $linkSet = $this->_linkDataMapper->findLinkByType($linkTypeSet);

        $resultSet = array();

        if($linkTypeSet->count()){
            foreach ($linkTypeSet as $linkType) {
                $type = $this->_linkTypeDataMapper->mapper($linkType);
                $linkTree = new stdClass();
                $linkTree->type = $type;
                if(array_key_exists($linkType->type_id, $linkSet)){
                    $linkTree->links = $linkSet[$linkType->type_id];
                }
                $resultSet[] = $linkTree;
            }
        }else{
            $resultSet = null;
        }

        return $resultSet;
    }

    /**
     * 保存链接
     * @param array $linkData
     * @param integer $primaryId
     * @return
     */
    public function saveLink(array $linkData, $primaryId = 0)
    {
        return $this->_linkDataMapper->save($linkData, $primaryId);
    }

    /**
     * 保存链接类型
     * @param array $linkTypeData
     * @param integer $primaryId
     * @return
     */
    public function saveLinkType(array $linkTypeData, $primaryId = 0)
    {
        return $this->_linkTypeDataMapper->save($linkTypeData, $primaryId);
    }

    /**
     * 删除友情链接
     * @param integer $primaryId
     * @return
     */
    public function delLink($primaryId)
    {
        $primaryId = (int) $primaryId;
        return $this->_linkDataMapper->delete($primaryId);
    }

    /**
     * 删除友情链接分类
     * @param integer $primaryId
     * @return
     */
    public function delLinkType($primaryId)
    {
        $primaryId = (int) $primaryId;
        return $this->_linkTypeDataMapper->deleteDependent($primaryId);
    }
}